package org.penguin.study.bk.controller

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import org.penguin.study.bk.base.api.Return
import org.penguin.study.bk.base.api.Return.Companion.ok
import org.penguin.study.bk.constant.CommonConstant
import org.penguin.study.bk.entity.Message
import org.penguin.study.bk.service.MessageService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource

//@Api(tags = ["消息表"])
@RestController
@RequestMapping("/message")
class MessageController {
    val log: Logger by lazy { LoggerFactory.getLogger(MessageController::class.java) }

    @Resource
    lateinit var messageService: MessageService

    @Operation(summary = "消息表-使用JPA保存消息", description = "消息表-使用JPA保存消息")
    @PostMapping("/saveByJPA")
    fun saveOrUpdateByJPA(@Parameter(description = "要保存的消息") @RequestBody message: Message) = Return.ok(messageService.saveOrUpdateByJPA(message))


    @Operation(summary = "消息表-使用MyBatisPlus保存消息", description = "消息表-使用MyBatisPlus保存消息")
    @PostMapping("/saveByPlus")
    fun saveOrUpdateByPlus(@RequestBody message: Message): Return<*> = Return.ok(messageService.saveOrUpdateByPlus(message))

    @Operation(summary = "消息表-使用MyBatisPlus分页", description = "消息表-使用MyBatisPlus分页")
    @GetMapping("/plusPage")
    open fun plusPage(
        @Parameter(description = "页码", ref = CommonConstant.PAGE_DEFAULT) @RequestParam(
            required = false,
            defaultValue = CommonConstant.PAGE_DEFAULT
        ) page: Long?,
        @Parameter(description = "每页记录数", ref = CommonConstant.PAGESIZE_DEFAULT) @RequestParam(
            required = false,
            defaultValue = CommonConstant.PAGESIZE_DEFAULT
        ) pageSize: Long?
    ): Return<*> {
        log.debug("页码:{}, 每页记录数:{}", page, pageSize)
        return ok(
            messageService.plusPageByQW(
                Page(
                    page!!, pageSize!!
                ), null
            )
        )
    }
}